package com.project.light.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.light.dto.ConfigDto;
import com.project.light.dto.PermissionParam;
import com.project.light.dto.PremissionResult;
import com.project.light.util.HttpUtil;
import com.project.light.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/lighting-rest/api/v1/permission/")
public class PermissionController {

    @Resource
    ConfigDto configDto;

    @PostMapping("Request/post")
    public RestResult permissionRequest(@RequestHeader(value = "authorization") String token, @RequestBody PermissionParam param) {
        PremissionResult result;
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/permission/Request";
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            result = JSONObject.parseObject(JSON.toJSONString(httpUtil.postUrl(param)), PremissionResult.class);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok(result);
    }

    @PostMapping("Release/post")
    public RestResult permissionRelease(@RequestHeader(value = "authorization") String token, @RequestBody PermissionParam param) {
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/permission/Release";
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            httpUtil.postUrl(param);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok();
    }

    @PostMapping("QueryStatus/post")
    public RestResult permissionQueryStatus(@RequestHeader(value = "authorization") String token, @RequestBody PermissionParam param) {
        PremissionResult result;
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/permission/QueryStatus?sysId=" + param.getSysId();
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            result = JSONObject.parseObject(JSON.toJSONString(httpUtil.getUrl()), PremissionResult.class);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok(result);
    }
}
